package mitm;

import java.net.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;

class MITMAdminServer implements Runnable
{
    private ServerSocket m_serverSocket;
    private Socket m_socket = null;
    private HTTPSProxyEngine m_engine;

    public MITMAdminServer( String localHost, int adminPort, HTTPSProxyEngine engine ) throws IOException {
        MITMPlainSocketFactory socketFactory =
            new MITMPlainSocketFactory();
        m_serverSocket = socketFactory.createServerSocket( localHost, adminPort, 0 );
        m_engine = engine;
    }

    public void run() {
        System.out.println("Admin server initialized, listening on port " + m_serverSocket.getLocalPort());
        while( true ) {
            try {
                m_socket = m_serverSocket.accept();

                byte[] buffer = new byte[40960];

                Pattern userPwdPattern =
                    Pattern.compile("username:(\\S+)\\s+password:(\\S+)\\s+command:(\\S+)\\sCN:(\\S*)\\s");

                BufferedInputStream in =
                    new BufferedInputStream(m_socket.getInputStream(),
                            buffer.length);

                // Read a buffer full.
                int bytesRead = in.read(buffer);

                String line =
                    bytesRead > 0 ?
                    new String(buffer, 0, bytesRead) : "";

                Matcher userPwdMatcher =
                    userPwdPattern.matcher(line);

                // parse username and pwd
                if (userPwdMatcher.find()) {
                    String userName = userPwdMatcher.group(1);
                    String password = userPwdMatcher.group(2);

                    // authenticate
                    // if authenticated, do the command
                    boolean authenticated = true;
                    if( authenticated ) {
                        String command = userPwdMatcher.group(3);
                        String commonName = userPwdMatcher.group(4);

                        doCommand( command );
                    }
                }	
            }
            catch( InterruptedIOException e ) {
            }
            catch( Exception e ) {
                e.printStackTrace();
            }
        }
    }

    // TODO implement the commands
    private void doCommand( String cmd ) throws IOException {

        m_socket.close();

    }

}
